﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace LiveS3D
{
    public partial class WebcamChooser : ChildWindow
    {
        public WebcamChooser()
        {
            InitializeComponent();
            this.Closed += new EventHandler(WebcamChooser_Closed);
        }

        void WebcamChooser_Closed(object sender, EventArgs e)
        {
            Source.Stop();
        }

        public CaptureSource Source { get; private set; }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void WebCams_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (Source != null) Source.Stop();

            Source = new CaptureSource();
            Source.VideoCaptureDevice = e.AddedItems[0] as VideoCaptureDevice;
            var brush = new VideoBrush() { Stretch = Stretch.Uniform };
            brush.SetSource(Source);
            CamVideo.Fill = brush;
            Source.Start();
        }
    }
}

